import { Injectable } from '@angular/core';
// import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the AppGlobalProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class AppGlobal {

    private static instance: AppGlobal = new AppGlobal();

    public static getInstance(): AppGlobal {
        return AppGlobal.instance;
    }
    
    constructor() {
        //console.log('Hello AppGlobalProvider Provider');
        // console.log('Hello AppGlobal Provider');
        if (AppGlobal.instance) {
            throw new Error("错误: 请使用AppGlobal.getInstance() 代替使用new.");
        }
        AppGlobal.instance = this;
    }
   

    userInfo: any;
    roleButtons: any;
    badTypes: any;
    roleResources: any;


    //设置token
    setToken(token) {
        window.localStorage['answ_iot_app'] = token;
    }

    //获取token
    getToken() {
        return window.localStorage['answ_iot_app'];
    }

    //设置用户权限按钮
    setRoleButtons(roleButtons) {
        this.roleButtons = roleButtons;
    }

    //获取用户权限按钮
    getRoleButtons() {
        return this.roleButtons;
    }

    //设置用户信息
    setUserInfo(info) {
        this.userInfo = info;
    }

    //获取用户信息
    getUserInfo() {
        return this.userInfo;
    }

    //设置故障类型 json
    setBadTypes(info) {
        this.badTypes = info;
    }

    //获取故障类型
    getBadTypes() {
        return this.badTypes;
    }

    //设置权限路由编码信息
    setRoleResources(info) {
        this.roleResources = info;
    }

    //获取权限路由编码信息
    getRoleResources() {
        return this.roleResources;
    }

    //清除用户信息和token
    clearTokenAndUser() {
        this.userInfo = null;
        localStorage.removeItem('answ_iot_app');
    }

}
